package com.bnlr.lrparking.appui.order;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.bnlr.lrparking.app.Application;
import com.bnlr.lrparking.R;
import com.bnlr.lrparking.appui.order.stages.ByStagesDetailsActivity;
import com.bnlr.lrparking.appui.web.CarWebActivity;
import com.bnlr.lrparking.base.BaseActivity;
import com.bnlr.lrparking.app.AppConfig;
import com.bnlr.lrparking.appui.order.mode.OrderIntentBean;
import com.bnlr.lrparking.appui.order.mode.StagesStepBean;
import com.bnlr.lrparking.constant.LrConstant;
import com.bnlr.lrparking.constant.Urls;
import com.bnlr.lrparking.view.CarCommon;
import com.bnlr.lrparking.widget.SuperTextView;
import com.bnlr.lrparking.widget.step.StepView;
import com.bnlr.nohttp.CallServer;
import com.bnlr.nohttp.CustomHttpListener;
import com.yolanda.nohttp.NoHttp;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 分期进度查询
 * @author tian
 */
public class StagesStepActivity extends BaseActivity {
    @BindView(R.id.step_view)
    StepView stepView;
    List<StepItemData> datas = new ArrayList<>();
    @BindView(R.id.tv_nav_title)
    TextView tvtile;
    @BindView(R.id.stv_order_call)
    SuperTextView tvCall;
    @BindView(R.id.stv_again_apply)
    SuperTextView tvApply;
    @BindView(R.id.stv_order_example)
    SuperTextView tvExample;
    private StagesStepBean.DataBean stepBean;
    private Unbinder unBinder;
    private OrderIntentBean item;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stages_step);
        unBinder = ButterKnife.bind(this);
        tvtile.setText("分期进度");
        item = getIntent().getParcelableExtra("order_mode");
        initListener();
        initData();
    }

    /**
     * 设置监听
     */
    private void initListener() {
        // 打电话
        tvCall.setOnTextViewClickListener(new SuperTextView.OnTextViewClickListener() {
            @Override
            public void onClick(View v) {
                CarCommon carCommon = new CarCommon();
                carCommon.phoneDialog(StagesStepActivity.this);
            }
        });
        // 重新申请
        tvApply.setOnTextViewClickListener(new SuperTextView.OnTextViewClickListener() {
            @Override
            public void onClick(View v) {
               if (stepBean != null) {
                   startActivity(new Intent(StagesStepActivity.this, ByStagesDetailsActivity.class)
                           .putExtra("enter_stages_from", 2).putExtra("order_mode",item));
               }
            }
        });
        // 办理分期
        tvExample.setOnTextViewClickListener(new SuperTextView.OnTextViewClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(StagesStepActivity.this,CarWebActivity.class)
                        .putExtra(CarWebActivity.WEB_TAG,0)
                );
            }
        });
    }


    private void initData() {
        try {
            mRequest = NoHttp.createStringRequest(Urls.BANK_STAGE, LrConstant.POST);
            mRequest.add("action", "GetBankStageStatus");
            mRequest.add("uid", AppConfig.getInstance().getString("uid", ""));
            mRequest.add("phonePara", Application.DEVICE_ID);
            mRequest.add("orderId", item.getId());
            // 添加到请求队列+
            CallServer.getRequestInstance().add(this, mRequest,
                    new CustomHttpListener<StagesStepBean>(this, true, StagesStepBean.class) {
                        @Override
                        public void doWork(StagesStepBean data, String code) {
                            if (TextUtils.equals("1", code)) {
                                datas.clear();
                                stepBean = data.getData();
                                for (int i = 0; i < stepBean.getCharts().size(); i++) {
                                    StepItemData ssb = new StepItemData(
                                            stepBean.getCharts().get(i).getDiscription(),
                                            stepBean.getCharts().get(i).getCreateTime());
                                    datas.add(ssb);
                                }
                                if (stepBean.getIsSuccess() == 0) {
                                    stepView.setHighDrawable(getResources().getDrawable(R.drawable.staging_failure));
                                } else {
                                    stepView.setHighDrawable(getResources().getDrawable(R.drawable.staging_success));
                                    tvApply.setVisibility(View.GONE);
                                }
                                initStep();
                            } else {
                                ToastUtils.showShort(data.getMsg()+"");
                            }
                        }
                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {

                        }
                    }, true);
        } catch (Exception e) {
        }
    }
    private void initStep() {
        // 指定数据集合
        stepView.setDatas(datas);
        // 设置view的绑定监听
        stepView.setBindViewListener(new StepView.BindViewListener() {
            @Override
            public void onBindView(TextView itemMsg, TextView itemDate, Object data, int position) {
                StepItemData sid = (StepItemData) data;
                if (position == 0) {
                    if (stepBean.getIsSuccess() == 0) {
                        itemMsg.setText(sid.getMsg());
                        itemMsg.setTextColor(Color.parseColor("#ea5544"));
                    } else {
                        itemMsg.setText(sid.getMsg());
                        itemMsg.setTextColor(Color.parseColor("#00CAA3"));
                    }
                } else {
                    itemMsg.setText(sid.getMsg());
                }
                itemDate.setText(sid.getDate());
            }

        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unBinder.unbind();
    }
}
